
#include <BStream>
#include "member_BXmlNode.h"

using namespace BWE;

XmlFilter::XmlFilter(const BString& desc)
{
	int pos = desc.word(name, 0);

	BStringList strlst;
	int begin = pos;
	int end = pos;
	while(end <= desc.size())
	{
		if (desc[end] == ':' || desc[end] == '&' || desc[end] == '|' || desc[end] == 0)
		{
			if (end > begin)
			{
				strlst.append(BString());
				BString& str = strlst.last();
				desc.substr(str, begin, end);
			}
			begin = end;
		}
		end++;
	}

	for (int i = 0; i < strlst.size(); i++)
	{
		BString text = strlst[i];
		if (text.beginsWidth(':'))
		{
			attribs.append(AttribPair());
			AttribPair& attrib = attribs.last();
			int name_end = text.word(attrib.name, 1);
			if (name_end < text.size())
			{
				BString equal;
				int equal_end = text.word(equal, name_end);
				if (equal == '=')
				{
					text.substr(attrib.value, equal_end);
					attrib.value.trimmed();
					int a = 0;
				}
			}
			continue;
		}
		if (text.beginsWidth('&'))
		{
			includeTexts.append(text.cstr() + 1);
		}
		if (text.beginsWidth('|'))
		{
			excludeTexts.append(text.cstr() + 1);
		}
	}

}

bool XmlFilter::operator()(BXmlNode* node) const
{
	if (node->name() != name)
		return false;
	bool attrib_match = true;
	for (int ai = 0; ai < attribs.size(); ai++)
	{
		const AttribPair& attrib = attribs[ai];
		if (!node->hasAttrib(attrib.name))
		{
			attrib_match = false;
			return false;
		}
		if (attrib.value.size() && node->attrib(attrib.name) != attrib.value)
		{
			attrib_match = false;
			return false;
		}
	}
	if (!attrib_match)
		return false;
	return true;
}

member_BXmlNode::member_BXmlNode(BXmlNode* xmlNode)
{
	boss = xmlNode;
	parent = 0;
	dirty = false;

	head_pre = 0;
	head_post = 0;
	head_in_pre = 0;
	head_in_post = 0;

	tail_pre = 0;
	tail_post = 0;
	tail_in_pre = 0;
	tail_in_post = 0;
}
member_BXmlNode::~member_BXmlNode()
{

}
